package cn.xwlin.controller;

import cn.xwlin.consts.Result;
import cn.xwlin.consts.TicketStatusConstant;
import cn.xwlin.entity.TicketInProgressInfo;
import cn.xwlin.mapper.TicketInProgressInfoMapper;
import cn.xwlin.service.CreateMatchService;
import cn.xwlin.util.HttpUtils;
import cn.xwlin.vo.UserResponse;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ticket")
public class GetTicketController {

    @Autowired
    private TicketInProgressInfoMapper ticketInProgressInfoMapper;
    @Autowired
    private CreateMatchService createMatchService;


    @RequestMapping("/initMatchUser")
    public Result loginRefresh(String userId, String userName) {
        if (!StringUtils.hasLength(userId) || !StringUtils.hasLength(userName)) {
            return Result.failure("参数为空");
        }
        TicketInProgressInfo ticketInProgressInfo = ticketInProgressInfoMapper.selectByUserId(userId);
        if (ticketInProgressInfo == null) {
            return Result.failure("当前UserId数据不存在");
        }
        String matchUser = HttpUtils.getMatchUser(ticketInProgressInfo.getUserId(), ticketInProgressInfo.getLoginToken(), ticketInProgressInfo.getHeaderInfo());
        if (!StringUtils.hasLength(matchUser)) {
            return Result.failure("获取观赛人信息请求返回失败，为空");
        }
        System.out.println(matchUser);
        UserResponse userResponse = JSONObject.parseObject(matchUser, UserResponse.class);
        if (userResponse.getCode() != 1) {
            return Result.failure("获取观赛人信息请求返回失败，code!=1, = " + userResponse.getCode());
        }
        List<UserResponse.UserInfo> allUser = userResponse.getData();
        if (CollectionUtils.isEmpty(allUser)) {
            return Result.failure("获取观赛人信息请求返回失败，data为空");
        }
        String[] split = userName.split(",");
        if (split.length > 2) {
            return Result.failure("最多允许两个观赛人");
        }
        List<UserResponse.UserInfo> needBuyUser = Lists.newArrayList();
        for (String toBuyUser : split) {
            for (UserResponse.UserInfo userInfo : allUser) {
                if (toBuyUser.equalsIgnoreCase(userInfo.getRealname())) {
                    needBuyUser.add(userInfo);
                }
            }
        }
        if (needBuyUser.isEmpty()) {
            return Result.failure("没有找到需要购买的用户姓名");
        }
        if (split.length != needBuyUser.size()) {
            return Result.failure("传入人数与匹配人数不相等");
        }
        TicketInProgressInfo updateInfo = new TicketInProgressInfo();
        updateInfo.setId(ticketInProgressInfo.getId());
        updateInfo.setViewInfo(JSONObject.toJSONString(needBuyUser));
        updateInfo.setStatus(TicketStatusConstant.STATUS_IN_PROGRESS);
        updateInfo.setNextCheckTime(new Date());
        ticketInProgressInfoMapper.updateByPrimaryKeySelective(updateInfo);
        return Result.success();
    }


    @RequestMapping("/createMatchTicket")
    public Result createMatchTicket(String userId) {
        if (!StringUtils.hasLength(userId)) {
            return Result.failure("参数为空");
        }
        TicketInProgressInfo ticketInProgressInfo = ticketInProgressInfoMapper.selectByUserId(userId);
        if (ticketInProgressInfo == null) {
            return Result.failure("当前UserId数据不存在");
        }
        if (!StringUtils.hasLength(ticketInProgressInfo.getRegionInfo())) {
            return Result.failure("没有区域信息");
        }
        String[] split = ticketInProgressInfo.getRegionInfo().split(",");
        for (String regionUuid : split) {
            Result match = createMatchService.createMatch(ticketInProgressInfo.getId(), Long.valueOf(regionUuid));
            System.out.println("userId:" + ticketInProgressInfo.getUserId() + ",region:" + regionUuid + ",result:" + JSON.toJSONString(match));
        }
        return Result.success();
    }
}
